package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Update extends JInternalFrame {
	private JTextField code;
	private JButton btnSearch;
	private JLabel label;
	private JLabel lblNewLabel;
	private JTextField ea;
	private JButton butUpdate;
	private JLabel status;
	private JLabel lblNewLabel_2;
	private JTextField serial;
	
	Connection conn;
	DatabaseTest main;

	
	private JButton btnDel;
	

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Update frame = new Update();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Update(Connection c, DatabaseTest m) {
		super("수정", true, true, true, false);
		setBounds(300, 0, 317, 350);
		getContentPane().setLayout(null);
		getContentPane().add(getCode());
		getContentPane().add(getBtnSearch());
		getContentPane().add(getLabel());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getEa());
		getContentPane().add(getButUpdate());
		getContentPane().add(getStatus());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getSerial());
		getContentPane().add(getBtnDel());
		
		this.conn = c;
		this.main = m;

	}
	
	public void setData(String s,String c, String e){ //조회화면에서 테이블행 클릭했을때, 수정화면에 해당코드출력.
		serial.setText(s);
		code.setText(c);
		ea.setText(e);
	}
	
	
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(77, 38, 125, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("검색");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s = Integer.parseInt(serial.getText());
					String sql = "select * from ptbl where serial = ?";
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s); // 1번째 ?에 s값 넣어라.
						
						ResultSet rs = pstmt.executeQuery();  //쿼리를 날린다. insert에서는 executeUpdate 날렸음.
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText("" + rs.getInt("ea"));							
						}else{
							status.setText("데이터가 없습니다.");	
							code.setText("");
							ea.setText("");
							serial.requestFocus();
						}
						
						rs.close();
						pstmt.close();
						
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					
				}
			});
			btnSearch.setBounds(209, 9, 63, 23);
		}
		return btnSearch;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드:");
			label.setBounds(17, 41, 41, 15);
		}
		return label;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("수량: ");
			lblNewLabel.setBounds(17, 68, 41, 15);
		}
		return lblNewLabel;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(77, 65, 125, 21);
		}
		return ea;
	}
	private JButton getButUpdate() {
		if (butUpdate == null) {
			butUpdate = new JButton("수정");
			butUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent ev) {
					int s = Integer.parseInt(serial.getText());
					String c = code.getText();
					int e = Integer.parseInt(ea.getText());
					String sql = "update ptbl set code = ?, ea = ? where serial = ?";
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,c);
						pstmt.setInt(2,e);
						pstmt.setInt(3,s);
						
						pstmt.executeUpdate();
						
						pstmt.close();
						
						
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					
				}
			});
			butUpdate.setBounds(105, 96, 97, 23);
		}
		return butUpdate;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("검색할 코드를 입력하세요.");
			status.setHorizontalAlignment(SwingConstants.CENTER);
			status.setBounds(12, 252, 259, 15);
		}
		return status;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("시리얼:");
			lblNewLabel_2.setBounds(17, 13, 48, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(77, 11, 125, 21);
		}
		return serial;
	}
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton("삭제");
			btnDel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					
				}
			});
			btnDel.setBounds(105, 129, 97, 23);
		}
		return btnDel;
	}
}
